package com.zbkj.front.controller.merchant;

import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.traceability.Batches;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.traceability.BatchesPageRequest;
import com.zbkj.common.request.traceability.BatchesRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.BatchesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 商品批次管理控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: xiaolic
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/merchant/batches")
@Api(tags = "商品溯源-批次管理")
public class BatchesController {

    @Autowired
    private BatchesService batchesService;

    /**
     * 分页查询商品批次列表
     * @param request 查询参数
     * @return 批次列表
     */
    @ApiOperation(value = "分页查询商品批次列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<Batches>> getList(@Validated BatchesPageRequest request) {
        PageInfo<Batches> pageInfo = batchesService.getPageList(request);
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 添加商品批次
     * @param request 批次信息
     * @return 添加结果
     */
    @ApiOperation(value = "添加商品批次")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Validated BatchesRequest request) {
        if (batchesService.create(request)) {
            return CommonResult.success("添加成功");
        } else {
            return CommonResult.failed("添加失败");
        }
    }

    /**
     * 修改商品批次
     * @param request 批次信息
     * @return 修改结果
     */
    @ApiOperation(value = "修改商品批次")
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody @Validated BatchesRequest request) {
        if (request.getId() == null) {
            return CommonResult.failed("批次ID不能为空");
        }
        if (batchesService.update(request)) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    /**
     * 删除商品批次
     * @param id 批次ID
     * @return 删除结果
     */
    @ApiOperation(value = "删除商品批次")
    @ApiImplicitParam(name = "id", value = "批次ID", required = true, dataType = "Long", paramType = "path")
    @PostMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable Long id) {
        if (batchesService.delete(id)) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.failed("删除失败");
        }
    }

    /**
     * 获取商品批次详情
     * @param id 批次ID
     * @return 批次详情
     */
    @ApiOperation(value = "获取商品批次详情")
    @ApiImplicitParam(name = "id", value = "批次ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/detail/{id}")
    public CommonResult<Batches> getDetail(@PathVariable Long id) {
        Batches detail = batchesService.getDetail(id);
        if (detail != null) {
            return CommonResult.success(detail);
        } else {
            return CommonResult.failed("批次不存在");
        }
    }

    /**
     * 检查号段是否重叠
     * @param startSerial 开始号段
     * @param endSerial 结束号段
     * @param productId 商品ID
     * @param merchantId 商家ID
     * @param batchId 排除的批次ID（可选，更新时使用）
     * @return 检查结果
     */
    @ApiOperation(value = "检查号段是否重叠")
    @GetMapping("/check-serial-overlap")
    public CommonResult<Boolean> checkSerialOverlap(
            @RequestParam Long startSerial,
            @RequestParam Long endSerial,
            @RequestParam Integer productId,
            @RequestParam Integer merchantId,
            @RequestParam(required = false) Long batchId) {
        Boolean isOverlap = batchesService.checkSerialOverlap(startSerial, endSerial, productId, merchantId, batchId);
        return CommonResult.success(isOverlap);
    }
}
